package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.domain.Page;
import com.rules.admin.entity.ReportConditionModel;
import com.rules.admin.entity.ReportModel;
import com.rules.admin.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RequestMapping({"/monitor"})
@Controller
public class ReportController {
   @Autowired
   private ReportService reportService;

   @RequestMapping(
      value = {"/report"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Page getReport(ReportConditionModel model, Page page) {
      if ("".equals(model.getProjectName())) {
         model.setProjectName((String)null);
      }

      return this.reportService.getPage(model, page);
   }

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<ReportModel> getReport() throws ParseException {
      ReportConditionModel model = new ReportConditionModel();
      SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
      SimpleDateFormat formater2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date startTime = formater2.parse(formater.format(new Date()) + " 00:00:00");
      Date endTime = formater2.parse(formater.format(new Date()) + " 23:59:59");
      model.setStartTime(startTime);
      model.setEndTime(endTime);
      return this.reportService.getReports(model);
   }

   @RequestMapping(
      value = {"/errLog"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Page getErrLog(ReportConditionModel model, Page page) {
      return this.reportService.getErrLog(model, page);
   }
}
